﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Dogshow.Model;
using Dogshow.Web.Services;
using Dogshow.Web.Utils;

namespace Dogshow.Web.Controllers
{
    public class KinoloskaDrustvaController : Controller
    {
        private IRepoServices m_Repository;

        public KinoloskaDrustvaController(IRepoServices repository)
        {
            m_Repository = repository;
        }

        public ActionResult Index()
        {
            var kinoloskaDrustva = m_Repository.VratiKinoloskaDrustva();
            HttpContext.Application["KinoloskaDrustva"] = kinoloskaDrustva;
            ViewBag.KinoloskaDrustva = kinoloskaDrustva;
            return View(new KinoloskoDrustvo{});
        }

        

        [HttpPost]
        public ActionResult Azuriraj([ModelBinder(typeof(DevExpressEditorsBinder))]KinoloskoDrustvo kinoloskoDrustvo)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.KinoloskaDrustva = HttpContext.Application["KinoloskaDrustva"];
                return View("Index", kinoloskoDrustvo);
            }
            else
            {
                m_Repository.SacuvajKinoloskoDrustvo(kinoloskoDrustvo);
                return RedirectToAction("Index");
            }
        }

        public PartialViewResult VratiKinoloskaDrustva()
        {
            var kinoloskaDrustva = (IEnumerable<KinoloskoDrustvo>)HttpContext.Application["KinoloskaDrustva"];
            ViewBag.KinoloskaDrustva = kinoloskaDrustva;
            return PartialView("KinoloskaDrustvaGrid");
        }

        public PartialViewResult VratiKinoloskoDrustvo(int? id)
        {
            var kd = new KinoloskoDrustvo();
            if (id.HasValue)
            {
                kd = m_Repository.VratiKinoloskoDrustvoPoId(id.Value);
            }
            return PartialView("KinoloskoDrustvoCallbackPanel", kd);
        }
    }
}
